package com.linuxgt.android.fileapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.linuxgt.android.fileapp.service.FileService;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText content = (EditText) findViewById(R.id.file_content);
        FileService fileService = new FileService(getApplicationContext());
        try {
            content.setText(fileService.read("test"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Button button = (Button) findViewById(R.id.button);
        Button buttonSD = (Button) findViewById(R.id.buttonSD);
        button.setOnClickListener(new ButtonOnClickListener());
        buttonSD.setOnClickListener(new ButtonSDOnClickListener());
    }

    private class ButtonOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            EditText fileNameET = (EditText) findViewById(R.id.file_name);
            EditText fileContentET = (EditText) findViewById(R.id.file_content);
            String fileName = String.valueOf(fileNameET.getText());
            String fileContent = String.valueOf(fileContentET.getText());
            FileService fileService = new FileService(getApplicationContext());
            try {
                fileService.save(fileName, fileContent);
                Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private class ButtonSDOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            EditText fileNameET = (EditText) findViewById(R.id.file_name);
            EditText fileContentET = (EditText) findViewById(R.id.file_content);
            String fileName = String.valueOf(fileNameET.getText());
            String fileContent = String.valueOf(fileContentET.getText());
            FileService fileService = new FileService(getApplicationContext());
            try {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)) {
                            Toast.makeText(MainActivity.this,
                                    R.string.permission, Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
                        } else {
                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS}, 1);
                        }
                    } else {
                        fileService.saveToSDCard(fileName, fileContent);
                        Toast.makeText(MainActivity.this, R.string.SDsuccess, Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, R.string.fail, Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
